package edu.ccut.saturn.studio.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnRouterDialog extends TitleAreaDialog {
	
	private Text textDec;
	private Text textName;
	private Shape shape;
	public SaturnRouterDialog(Shell parentShell , Shape shape) {
		super(parentShell);
		this.shape = shape;
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + shape.getName()); 
		return contents;
	}
	
	protected Control createDialogArea(Composite parent) {
		
		Composite area = (Composite) super.createDialogArea(parent);
		final Composite composite = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, false, false);
		gd_composite.heightHint = 127;
		gd_composite.widthHint = 398;
		composite.setLayoutData(gd_composite);

		final Label label = new Label(composite, SWT.NONE);
		final GridData gd_label = new GridData();
		gd_label.verticalIndent = 5;
		label.setLayoutData(gd_label);
		label.setText(Messages.getString("SaturnRouterDialog.name")); //$NON-NLS-1$

		textName = new Text(composite, SWT.BORDER);
		textName.setTextLimit(50);
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textName.verticalIndent = 5;
		textName.setLayoutData(gd_textName);

		final Label label_1 = new Label(composite, SWT.NONE);
		final GridData gd_label_1 = new GridData();
		gd_label_1.verticalIndent = 5;
		label_1.setLayoutData(gd_label_1);
		label_1.setText(Messages.getString("SaturnRouterDialog.description")); //$NON-NLS-1$

		textDec = new Text(composite, SWT.V_SCROLL | SWT.BORDER);
		textDec.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textDec.verticalIndent = 5;
		gd_textDec.heightHint = 65;
		textDec.setLayoutData(gd_textDec);
		setMessage(Messages.getString("SaturnRouterDialog.AttributeEvaluate")); //$NON-NLS-1$
		
		textName.setText(this.shape.getName());
		textName.setSelection(this.shape.getName().length());
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if (SaturnValidation.isEmpty(textName.getText().trim())) { 
					setErrorMessage(Messages.getString("SaturnRouterDialog.NameIsEmpty"));  //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				} else {
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});
		
		textDec.setText(this.shape.getDescription());
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		return area;
	}
	
	protected Point getInitialSize() {
		return new Point(435, 260);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
		newShell.setText(Messages.getString("SaturnRouterDialog.SetParameter")); //$NON-NLS-1$
	}
	
	protected void buttonPressed(int buttonId) {
		if(buttonId==IDialogConstants.OK_ID){
			this.shape.setName(this.textName.getText().trim());
			this.shape.setDescription(this.textDec.getText().trim());
		}
		super.buttonPressed(buttonId);
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages
				.getString("ManageConnectionDialog.confirm"), false); //$NON-NLS-1$
		createButton(parent, IDialogConstants.CANCEL_ID, Messages
				.getString("ManageConnectionDialog.cancel"), false); //$NON-NLS-1$
	}
}